package ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.List;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;


public class CheckList_Universal extends JList
{
	private static Color Default_Color_Selection =  new Color(120,3,12);
	private static Color Default_Color_Checked   =  UIManager.getColor("List.textBackground");
	private static Color Default_Color_Not_Checked = UIManager.getColor("List.textBackground");
				
	private List Strings = null;
	private Vector Booleans = null;
	private Color Selection_Color = null;
	private Color Checked_Color = null;
	private Color Not_Checked_Color = null;
	
	HashSet<String> checkedValues = new HashSet<String>();
	HashSet<String> unCheckedValues = new HashSet<String>();
	
	protected List get_Strings(){
		if (Strings == null){
			Strings = new Vector<String>();
		}
		return Strings;
	}
	
	protected Vector get_Booleans(){
		if (Booleans == null){
			Booleans = new Vector();
		}
		return Booleans;
	}
	
	protected Color get_Selection_Color(){
		if (Selection_Color == null){
			Selection_Color = Default_Color_Selection;
		}
		return Selection_Color;
	}
	protected Color get_Checked_Color(){
		if (Checked_Color == null){
			Checked_Color = Default_Color_Checked;
		}
		return Checked_Color;
	}
	protected Color get_Not_Checked_Color(){
		if (Not_Checked_Color == null){
			Not_Checked_Color = Default_Color_Not_Checked;
		}
		return Not_Checked_Color;
	}
	
	protected void set_Strings(List<String> items){
		Strings = items;
	}
	protected void set_Booleans(Vector items){
		Booleans = items;
	}
	
	
	public void set_Color_Selection(Color new_Color){
		Selection_Color = new_Color;
	}
	
	public void set_Color_Checked(Color new_Color){
		Checked_Color = new_Color;
	}
	
	public void set_Color_not_Checked(Color new_Color){
		Not_Checked_Color = new_Color;
	}
	
	
    public CheckList_Universal(final List<String> items, final Vector item_checktable){
    	super(); //<-Konstruiert ne JList mit leerem Model->
    	set_Strings(items); //<-Setzt die Liste der Strings->
    	set_Booleans(item_checktable); //<-Setzt die Liste der Booleans->
    	init();
    }
    public CheckList_Universal(final List<String> items, final Vector item_checktable, Color Selektion_Color, Color Checked_Color, Color not_Checked_Color){
    	super(); //<-Konstruiert ne JList mit leerem Model->
    	set_Strings(items); //<-Setzt die Liste der Strings->
    	set_Booleans(item_checktable); //<-Setzt die Liste der Booleans->
    	set_Color_Selection(Selektion_Color);
    	set_Color_Checked(Checked_Color);
    	set_Color_not_Checked(not_Checked_Color);
    	init();
    }
    
    public Vector<String> getCheckedItems (){
    	return new Vector<String>(checkedValues);
    }
    
    public Vector<String> getUnCheckedItems (){
    	return new Vector<String>(unCheckedValues);
    }
    
    private void init()
    {
        setModel(new CheckListModel(Strings,Booleans));
        setCellRenderer(new CheckListRenderer());
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.setFixedCellHeight(18);
        System.out.println("Is enabled?:" + isEnabled());
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                int index = locationToIndex(e.getPoint());
                if (index != -1)
                {	
                    CheckListItem check = (CheckListItem) getModel()
                            .getElementAt(index);
                    if(isEnabled()){
                    	check.setChecked(!check.isChecked());
                    	if (Booleans.get(index).equals(false)){
                    		Booleans.insertElementAt(true, index);
                    		Booleans.remove(index+1);
                    		checkedValues.remove(check.getText());
                    		unCheckedValues.add(check.getText());
                    	}else if (Booleans.get(index).equals(true)){
                    		Booleans.insertElementAt(false, index);
                    		Booleans.remove(index+1);
                    		unCheckedValues.remove(check.getText());
                    		checkedValues.add(check.getText());
                    	}
                    }
                   	repaint();
                }
            }
        });
        
    }

    // --------- nested classes -----------------------

    protected class CheckListItem
    {    	
    	private boolean checked;

        private String  text = null;

        public CheckListItem(boolean isChecked, String text)
        {
        	this.checked = isChecked;
            this.text = text;
            
        }

        public boolean isChecked()
        {
            return this.checked;
        }

        public void setChecked(boolean isChecked)
        {
            this.checked = isChecked;
        }

        public String getText()
        {
            return this.text;
        }
    }

    @SuppressWarnings("serial")
    protected class CheckListRenderer extends JCheckBox implements
            ListCellRenderer
    {
        public CheckListRenderer()
        {
            setBackground(UIManager.getColor("List.textBackground"));
            setForeground(UIManager.getColor("List.textForeground"));
        }

        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus)
        {
            CheckListItem check = (CheckListItem) value;
           	setSelected(check.isChecked());	
            setText(check.getText());
            if (cellHasFocus){
            	this.setBackground(get_Selection_Color());
            } else {
            	if (check.isChecked()){
            		this.setBackground(get_Checked_Color());
            	}else {
            		this.setBackground(get_Not_Checked_Color());
            	}
            	
            }
            return this;
        }
    }
   
    @SuppressWarnings("serial")
    protected class CheckListModel extends AbstractListModel
    {
        private CheckListItem[] CLI = null;
       
        public CheckListModel(final List<String> Strings, final Vector Booleans)
        {
            CLI = new CheckListItem[Strings.size()];
            for(int i=0; i<Strings.size(); ++i)
            {
                CLI[i] = new CheckListItem(Booleans.get(i).toString().equals("true"), Strings.get(i));
            }
        }
       
        public int getSize()
        {
            return CLI.length;
        }

        public Object getElementAt(int index)
        {
            return CLI[index];
        }
    }
}
